# -*- coding: utf-8 -*-
import scrapy
import pymysql
from XCFScrapy.items import foodmaterialCategroyItem
from XCFScrapy.items import foodsItem
from XCFScrapy import settings

class FoodsspiderSpider(scrapy.Spider):
    name = 'foodsspider'
    allowed_domains = ['xiachufang.com']
    start_urls = ['http://xiachufang.com/']

    def parse(self, response):
       #到数据库获取所有的的食材数据
       foodmaterials_list =  self.foodcategroylocal_db()
       for item in foodmaterials_list:
           yield scrapy.Request(item['url'],meta={'item':item,'page':0},callback=self.requestFoodsMaterialWithData)


    """
    获取食材分类下对应的菜谱
    """
    def requestFoodsMaterialWithData(self,response):

        item = response.meta['item']
        starturl = item['url']+'?page='
        page = response.meta['page']

        if page < 5:
            page = page+1

        foods_pages = response.xpath("//div[@class='recipe recipe-215-horizontal pure-g image-link display-block']")
        for food in  foods_pages:

            #菜谱ID
            foodId = item['foodId']
            # 菜谱详情页
            foodDetailurl = food.xpath("./div/p[@class='name']/a/attribute::href").extract_first("")
            # 菜谱名称
            name = food.xpath("./div/p[@class='name']/a/text()").extract_first("").replace(" ","").strip()
            # 菜谱图片
            img = food.xpath("./a[1]/div/img/attribute::data-src").extract_first("")
            # 菜谱食材
            materialspan = food.xpath("./div/p[@class='ing ellipsis']/span/text()").extract()
            # 菜谱食材
            materiala =  food.xpath("./div/p[@class='ing ellipsis']/a/text()").extract()
            # 菜谱评价
            stats = food.xpath("./div/p[@class='stats']/span[1]/text()").extract_first("")
            # 做个这个菜谱的人数
            number = food.xpath("./div/p[@class='stats']/span[2]/text()").extract_first("")
            # 菜谱作者
            author =  food.xpath("./div/p[@class='author']/a/text()").extract_first("")
            materials = materialspan + materiala

            foodItem = foodsItem()

            foodItem['foodId'] = foodId
            foodItem['name'] = name
            foodItem['img'] = img
            foodItem['stats'] = stats
            foodItem['number'] = number
            foodItem['author'] = author
            foodItem['material'] = ",".join(materials)
            foodItem['foodDetailurl'] = "http://www.xiachufang.com"+foodDetailurl
            yield foodItem


        yield  scrapy.Request(starturl + str(page),meta={'item':item,'page':page},callback=self.requestFoodsMaterialWithData)
    """
    获取本地存储的食材分类数据
    """
    def foodcategroylocal_db(self):

        #打开数据库连接
        db = pymysql.connect(
            host=settings.MYSQL_HOST,
            db=settings.MYSQL_DBNAME,
            user=settings.MYSQL_USER,
            passwd=settings.MYSQL_PASSWD,
            charset='utf8',
            use_unicode=True)
        # 通过cursor执行增删查改
        cursor = db.cursor()

        #sql 查询语句
        sql = "select * from xiaChuFang_materialcategroy "

        try:
            #执行sql语句
            cursor.execute(sql)
            #获取所有记录
            results = cursor.fetchall()
            foodcagroy_list = []
            for row in results:
                item = foodmaterialCategroyItem()
                item['name'] = row[0]
                item['url'] = row[1]
                item['foodId'] = row[2]
                item['prentFoodId'] = row[3]
                foodcagroy_list.append(item)

            return foodcagroy_list
        except:
            print("Error: unable to fetch data")